//
//  ZHButton.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/3/2.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation

class ZHButton: SWSTAnswerButton {
    
    var isSelected:Bool = false{
        didSet{
            
        }
    }
    
    var iconImage:NSImage? {
        didSet {
            if let image = iconImage {
                self.icon.image = image
                self.icon.snp.updateConstraints { (make) in
                    make.size.equalTo(iconImage?.size ?? CGSize.zero)
                }
            }
            self.icon.isHidden = iconImage == nil
            
        }
    }
    
    lazy var icon: NSImageView = {
        let imageV = NSImageView.init(image: self.iconImage ?? NSImage.init())
        imageV.isHidden = true
        return imageV
    }()
    
    var backgroundViewColor:NSColor = NSColor.clear {
        didSet {
            self.backgraoundView.layer?.backgroundColor = backgroundViewColor.cgColor
        }
    }
    
    lazy var backgraoundView: NSView = {
        let view = NSView.init()
        view.wantsLayer = true
        view.layer?.backgroundColor = NSColor.clear.cgColor
        return view
    }()
    
    var zh_color:NSColor = NSColor.white.withAlphaComponent(0.01) {
        didSet {
            self.wantsLayer = true
            self.normalColor = zh_color
            self.highlightColor = zh_color
            self.hoverColor = zh_color
            self.selectedColor = zh_color
        }
    }
    
    var zh_backgroundColor:NSColor = NSColor.clear.withAlphaComponent(0.1) {
        didSet {
            self.backgroundNormalColor = zh_backgroundColor
            self.backgroundHighlightColor = zh_backgroundColor
            self.backgroundSelectedColor = zh_backgroundColor
            let hoverColor = zh_backgroundColor==NSColor.clear ? NSColor.clear:zh_backgroundColor.withAlphaComponent(0.8)
            self.backgroundHoverColor = hoverColor
        }
    }
    
    var zh_borderColor:NSColor = NSColor.init(calibratedRed: 1.0/255, green: 2.0/255, blue: 3.0/255, alpha: 0.01) {
        didSet {
            self.borderNormalColor = zh_borderColor
            self.borderHighlightColor = zh_borderColor
            self.borderHoverColor = zh_borderColor
            self.borderSelectedColor = zh_borderColor
        }
    }
    
    var zh_borderWidth:CGFloat = 1.0 {
        didSet {
            self.borderNormalWidth = zh_borderWidth
            self.borderHighlightWidth = zh_borderWidth
            self.borderHoverWidth = zh_borderWidth
            self.borderSelectedWidth = zh_borderWidth
        }
    }
    
    
    var zh_cornerRadius:CGFloat = 3.0 {
        didSet {
            self.cornerNormalRadius = zh_cornerRadius
            self.cornerHighlightRadius = zh_cornerRadius
            self.cornerHoverRadius = zh_cornerRadius
            self.cornerSelectedRadius = zh_cornerRadius
        }
    }
    
    
    var zh_font:NSFont = NSFont.systemFont(ofSize: 12.5) {
        didSet {
            self.wantsLayer = true
            self.font = zh_font
            self.title = zh_title
        }
    }
    
    var zh_title:String = "" {
        didSet {
            self.wantsLayer = true
            self.title = zh_title
            self.font = zh_font
        }
    }
    
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        self.zh_setupUI()
    }
    
    func zh_setupUI() {
        self.wantsLayer = true
        self.title = ""
        self.font = self.zh_font
        self.zh_color = NSColor.init(calibratedRed: 255.9/256.0, green: 255.9/256.0, blue: 255.9/256.0, alpha: 1.0/1.0)
        self.zh_backgroundColor = NSColor.clear
        self.backgroundHoverColor = NSColor.clear
        self.zh_borderColor = NSColor.clear
        self.zh_borderWidth = 0.0
        self.zh_cornerRadius = 0.0
        
        self.addSubview(backgraoundView, positioned: NSWindow.OrderingMode.below, relativeTo: self)
        backgraoundView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        self.addSubview(self.icon)
        icon.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize.zero)
            make.center.equalToSuperview()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


extension NSButton{
    //必须依赖于snapkit
    func makeToFit(font:CGFloat=12) {
        let width = self.title.ga_width(font: self.font?.pointSize ?? font)
        self.snp.makeConstraints { (make) in
            make.width.equalTo(width+10)
        }
    }
    
    func updateToFit(font:CGFloat=12) {
        let width = self.title.ga_width(font: self.font?.pointSize ?? font)
        self.snp.updateConstraints { (make) in
            make.width.equalTo(width+10)
        }
    }
    
    func updateToFit(font:CGFloat=12,string:String) {
        self.title = string
        let width = self.title.ga_width(font: self.font?.pointSize ?? font)
        self.snp.updateConstraints { (make) in
            make.width.equalTo(width+10)
        }
    }
    
    func ga_width(font:CGFloat=12) -> CGFloat {
        let width = self.title.ga_width(font: self.font?.pointSize ?? font)
        return width
    }
    
}

